package com.shawn.mobliesafe;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by shawn on 2016/4/7.
 */
public abstract class SetUpBaseActivity extends Activity {

    protected SharedPreferences sp;
    private GestureDetector gestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        sp = getSharedPreferences("config", MODE_PRIVATE);
        //1.获取手势识别器
        //要想让手势是识别器生效,必须将手势识别器注册到屏幕的触摸事件中
        gestureDetector = new GestureDetector(this,new MyOnGestureListener());
    }

    //界面的触摸事件
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        gestureDetector.onTouchEvent(event);
        return super.onTouchEvent(event);
    }

    private class MyOnGestureListener extends GestureDetector.SimpleOnGestureListener{
        //e1 : 按下的事件,保存有按下的坐标
        //e2 : 抬起的事件,保存有抬起的坐标
        //velocityX : velocity 速度    在x轴上移动的速率
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            //得到按下的x坐标
            float startX = e1.getRawX();
            //得到抬起的x坐标
            float endX = e2.getRawX();
            //得到按下的Y坐标
            float startY = e1.getRawY();
            //得到抬起的y坐标
            float endY = e2.getRawY();
            //判断是否是斜滑
            if(Math.abs(startY - endY) > 60) {
                return false;
            }
            //下一步
            if((startX - endX) > 100) {
                next_activity();
            }
            //上一步
            if ((endX-startX) > 100) {
                pre_activity();
            }
            return true;
        }
    }

    public void pre(View view){
        pre_activity();
    }

    public void next(View view){
        next_activity();
    }

    public abstract void next_activity();

    public abstract void pre_activity();

    //监听手机物理按钮的点击事件
    //keyCode :　物理按钮的标示
    //event : 按键的处理事件
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //判断keycode是否是返回键的标示
        if(keyCode == KeyEvent.KEYCODE_BACK) {
            //true:是可以屏蔽按键的事件
            //return true;
            pre_activity();
        }
        return super.onKeyDown(keyCode, event);
    }


}
